const Router = require('../../Router');
const resContent = require('../../../../util/ResValue')
const Validation = require('../../../../util/Validation');
const dbUtil = require("../../../../util/dbutil");
const SqlUpdate = require("../../../../util/SqlUpdate");

class WeatherRevise extends Router {

    weatherInfo = [
        "id",
        "cityId",
        "day7Info",
        "fxDate",
        "sunrise",
        "sunset",
        "moonrise",
        "moonset",
        "moonPhase",
        "moonPhaseIcon",
        "tempMax",
        "tempMin",
        "iconDay",
        "textDay",
        "iconNight",
        "textNight",
        "wind360Day",
        "windDirDay",
        "windScaleDay",
        "windSpeedDay",
        "wind360Night",
        "windDirNight",
        "windScaleNight",
        "windSpeedNight",
        "humidity",
        "precip",
        "pressure",
        "vis",
        "cloud",
        "uvIndex"
    ];

    constructor() {
        super('/super/weather-revise', 'post', false);
        //所有字段验证
        this.addIntermediateKey(this.check);
        //城市验证
        this.addIntermediateKey(Validation.isCity('cityId'));
        //天气验证
        this.addIntermediateKey(Validation.isWeather);
    }

    async handle(ctx, next) {
        let body = ctx.request.body;
        //不修改的几个的参数
        let parameter = JSON.parse(JSON.stringify(this.weatherInfo));
        //把json转换成字符串
        body.day7Info = JSON.stringify(body.day7Info);
        //新建修改
        const updateRes = await new SqlUpdate(`update weather set`, body)
            .setSearchCriteria(parameter)
            .setQueryCriteria(`where id = ${body.id}`)
            .execUpdate();
        ctx = await resContent.success(ctx);
        ctx.body.data = {
            modifyTheQuantity: updateRes
        };
    }


    //检查所需字段
    async check(ctx, next) {
        const body = ctx.request.body;
        const isLack = await Validation.theFieldIsMissingExists(body, this.weatherInfo);
        //有字段缺失
        if (isLack !== false) {
            ctx = await resContent.parametersAreMissing(ctx);
            ctx.body.message += `${isLack}`;
            return 0;
        }
        await next();
    }
}


module.exports = WeatherRevise;